﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using BML.Infrastructure.Interface;
using Model.BusinessLogic.PhysicalData;
using Model.Manager;
using Model.Manager.DataBase;
using SystmicFunction.Public.CharacterService;

namespace BML.Infrastructure.Implement
{

    public class WebAccountOp : PointTreeOp, IMember
    {
        internal Guid UserGuid { get; set; }
        internal WebAccount UserParent { get; set; }


        public WebAccount NewWebAccount(Guid id, Guid memberShipGuid, String account, String key, String invitCode)
        {
            return new WebAccount
            {
                Id = id,
                MemerShipGuid = memberShipGuid,
                Account = account,
                Key = key,
                InvitCode = invitCode,
            };
        }


        public void Create(String account, String key)
        {
            using (IDbContext db = ContextFactory.GetBlConn())
            {
                CreateWebAccount(account, key, db);
            }
        }

        public void CreateWebAccount(String account, String key, IDbContext db)
        {


            var memberShip = CreateMemberShip(db, account, key);
            CreatePointNode(db, memberShip.Id);
            Create(db.GetOperator<WebAccount>(), NewWebAccount(Guid.NewGuid(), memberShip.Id, account, Processor.Encryption(key), "邀请码"));
        }


    }

    /// <summary>
    ///  第三方的登录
    /// </summary>
    public class OAuthAccountOp : WebAccountOp
    {
        /// <summary>
        ///     生成模型对象
        /// </summary>
        /// <returns></returns>
        public OAuthAccount NewAuthAccount(String openId, Guid memberShipGuid, Int32 oauthType)
        {
            return new OAuthAccount { OpenId = openId, MemberShipGuid = memberShipGuid, OAuthType = oauthType };
        }

        public void Create()
        {
            using (IDbContext db = ContextFactory.GetBlConn())
            {
                CreateWebAccount("", "", db);
                db.GetOperator<OAuthAccount>().Create(NewAuthAccount("", Guid.Empty, OAuthAccount.OTqq));
            }
        }

    }


    
}
